﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CraftParameterConfig.Infrastructure.Data
{
    public class DbContext
    {
        public DbConnection GetDbConnection()
        {
            var connString = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;
            DbConnection dbConnection = new OracleConnection(connString); 
            return dbConnection;
        }
        public DbCommand GetDbCommand(DbConnection dbConnection)
        {
            DbCommand dbCommand = new OracleCommand();
            dbCommand.Connection = dbConnection as OracleConnection;
            return dbCommand;
        }
        public void ExecuteSqlNoQuery(DbCommand dbCommand, string sql)
        {
            dbCommand.CommandText = sql;
            dbCommand.ExecuteNonQuery();
        }
        public void ExecuteSqlNoQuery(DbCommand dbCommand, List<String> sqls)
        {
            foreach (var item in sqls)
            {
                dbCommand.CommandText = item;
                dbCommand.ExecuteNonQuery();
            }
        }
        public DataTable GetTable(DbCommand dbCommand, string sql)
        {
            OracleDataAdapter da = new OracleDataAdapter(dbCommand as OracleCommand);
            DataTable dtResult = new DataTable();
            ExecuteSqlNoQuery(dbCommand, sql);
            da.Fill(dtResult);
            return dtResult;
        }
    }
}
